package com.company.nuwa.redis;

import cn.hutool.extra.spring.SpringUtil;
import com.company.nuwa.redis.common.RedisConfiguration;
import com.company.nuwa.redis.support.cache.CacheBatchEvictAspect;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * <p>redis auto configuration</p >
 *
 * @author Nikola Tesla
 * @version 1.0
 * @date 2020/10/24 17:58
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@Import({SpringUtil.class, RedissonClientConfiguration.class, RedisCacheManagerConfiguration.class})
@AutoConfigureAfter({RedissonClientConfiguration.class, RedisCacheManagerConfiguration.class})
@AutoConfigureBefore({org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class})
public class RedisAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        ObjectMapper mapper = new ObjectMapper();
        // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(mapper, Object.class);
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(serializer);
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();
        log.info("load redis template...");
        return template;
    }
    @Bean
    @ConditionalOnMissingBean
    public RedisExtTemplate redisExtTemplate(RedissonConnectionFactory connectionFactory, RedisConfiguration redisConfiguration) {
        log.info("load redis ext template...");
        return new RedisExtTemplate(connectionFactory, redisConfiguration);
    }
    @Bean
    @ConditionalOnBean(RedisExtTemplate.class)
    public CacheBatchEvictAspect cacheBatchEvictAspect(RedisExtTemplate redisExtTemplate) {
        log.info("load redis catch batch evict aspect...");
        return new CacheBatchEvictAspect(redisExtTemplate);
    }

}
